﻿#include "HJMasterDevice.h"
#include "HJComm/HJFrame.h"

//#include "bsp/bsp_flash.h"
#include "bsp/bsp_uart3.h"
#include "app/app_comm.h"

class HJMasterDevice {

public:

    HJMasterDevice(const uint8_t* id, const uint8_t* version);

    virtual ~HJMasterDevice();

    long RxProcess(uint8_t* buff, uint16_t len);
    long TxJSONFrame(uint8_t* des_addr, uint8_t* buff, uint16_t len);
private:

    void TxTransmit(uint8_t* buff, uint16_t len);

    bool OnRecvOneFram(Frame* frame);
    FrameRecv m_recv;
    Frame m_frame;
    Frame m_tx_frame;
    uint8_t m_id[4];
    const uint8_t* m_version;
};

static uint8_t txbuff[1044];
static void DelayReboot(uint32_t time_ms);


static HJMasterDevice* pdev = NULL;

void HJDeiviceWrapInit(uint8_t id, uint8_t* version)
{
    static uint8_t m_id[4] = {0, 0, 0, id};
    static HJMasterDevice device(m_id, version);
    pdev = &device;

}

void HJDeiviceWrapRxProcess(void)
{
    uint8_t data;
    bsp_uart3_rx_transmit(&data, RT_WAITING_FOREVER);
    pdev->RxProcess(&data, 1);
}

void HJDeiviceWrapTxProcess(uint8_t* des_addr, uint8_t* buff, uint16_t len)
{
    pdev->TxJSONFrame(des_addr, buff, len);
}



void HJMasterDevice::TxTransmit(uint8_t* buff, uint16_t len)
{
    bsp_uart3_tx_transmit(buff, len);
}

HJMasterDevice::HJMasterDevice(const uint8_t* id, const uint8_t* version)
{
    memcpy(m_id, id, 4);
    m_version = version;
}

HJMasterDevice::~HJMasterDevice()
{
}

bool HJMasterDevice::OnRecvOneFram(Frame* frame)
{
    uint8_t addr[4] = {0};

    frame->cmd = JSON_TRANSFER_CMD;
    switch(frame->cmd)
    {
    case UPGRADE_EASE_CMD:
    {
//        frame->paload[0] = bsp_flash_ease_down_section();

        frame->cmd |= ACK_MASK;
        frame->paload_len = 1;
        break;
    }
    case UPGRADE_WRITE_CMD:
    {
        uint8_t* data = 0;
        uint16_t len = 0;
        uint32_t addr = 0;
        frame->unmake_write_pack(NULL, NULL, &addr, &data, &len);

//        frame->paload[0] = bsp_flash_write_down_section(addr, data, len);
        frame->cmd |= ACK_MASK;
        frame->paload_len = 1;
        break;
    }
    case UPGRADE_READ_CMD:
    {
        uint8_t* data = 0;
        uint16_t len = 0;
        uint32_t addr = 0;
        frame->unmake_read_pack(NULL, NULL, &addr, &len);

//        bsp_flash_read_down_section(addr, frame->paload, len);
        frame->cmd |= ACK_MASK;
        frame->paload_len = len;
        break;
    }
    case UPGRADE_REBOOT_CMD:
    {
        frame->paload[0] = 1;
        frame->cmd |= ACK_MASK;
        frame->paload_len = 1;
        DelayReboot(1000);
        break;
    }
    case UPGRADE_READ_VERSION:
    {
        frame->cmd |= ACK_MASK;
        frame->paload_len = strlen((const char*)m_version);
        memcpy(frame->paload, m_version, frame->paload_len);
        break;
    }
    case JSON_TRANSFER_CMD:
    {
        frame->cmd |= ACK_MASK;
        app_comm_process((char*)frame->paload, (char*)frame->paload, 1024);
        frame->paload_len = strlen((const char*)frame->paload);
        break;
    }
    default:
    {
        return false;   //默认和不应答的直接返回FALSE
    }
    }
    memcpy(addr, m_frame.src_addr, 4);
    memcpy(m_frame.src_addr, m_frame.des_addr, 4);
    memcpy(m_frame.des_addr, addr, 4);
    return true;
}

long HJMasterDevice::RxProcess(uint8_t* buff, uint16_t len)
{
    static const uint8_t broadcast_id[4] = {255, 255 , 255, 255};
    for (int i = 0; i < len; i++)
    {
        if (m_recv.proc_one_byte(&m_frame, buff[i]))
        {
//            qDebug("接收到一帧 处理帧");

//            if(memcmp(m_frame.des_addr, m_id, 4) != 0 &&
//               memcmp(m_frame.des_addr, broadcast_id, 4) != 0)
//                return false;                       //既不是本机id  也不是广播id 则退出

            uint16_t txlen = 0;

            if(OnRecvOneFram(&m_frame) == true)
            {
//                m_frame.make_pack(txbuff, &txlen);
//                TxTransmit(txbuff, txlen);
            }
            return true;
        }
    }
    return false;
}


long HJMasterDevice::TxJSONFrame(uint8_t* des_addr, uint8_t* buff, uint16_t len)
{
    uint16_t txlen = 0;

    m_tx_frame.head1 = 0xaa;
    m_tx_frame.head2 = 0x55;

    memcpy(m_tx_frame.src_addr, m_id , sizeof(m_id));
    memcpy(m_tx_frame.des_addr, des_addr , sizeof(des_addr));

    m_tx_frame.cmd = JSON_TRANSFER_CMD;

    m_tx_frame.end1 = 0x55;
    m_tx_frame.end2 = 0xaa;

    memcpy(m_tx_frame.paload, buff, len);
    m_tx_frame.paload_len = len;

    m_tx_frame.make_pack(txbuff, &txlen);
    TxTransmit(txbuff, txlen);
}




void HJDeiviceFrameHandleWrap(uint8_t id, uint8_t* version)
{
    uint8_t m_id[4] = {0, 0, 0, id};
    static HJMasterDevice device(m_id, version);

    uint8_t data;
    bsp_uart3_rx_transmit(&data, RT_WAITING_FOREVER);
    device.RxProcess(&data, 1);

}



/* 线程入口 */
static void recv_thread_entry(void* parameter)
{
    while (1)
    {
        HJDeiviceWrapRxProcess();
    }
}


void Recv_thread_init(void)
{
#define RECV_THREAD_STACK_SIZE   2048
#define RECV_THREAD_PRIORITY     10
#define RECV_THREAD_TIMESLICE    20

    rt_thread_t tid = RT_NULL;
    /* 创建线程 1 */
    tid = rt_thread_create("recv_thread",
                            recv_thread_entry, RT_NULL,
                            RECV_THREAD_STACK_SIZE,
                            RECV_THREAD_PRIORITY, RECV_THREAD_TIMESLICE);
    if (tid != RT_NULL)
        rt_thread_startup(tid);
}




/* 线程入口 */
static void thread_entry(void* parameter)
{
    rt_uint32_t value;

    value = (rt_uint32_t)parameter;
    while (1)
    {
        rt_thread_mdelay(value); // 延时一段时间
        rt_hw_cpu_reset();
    }
}


static void DelayReboot(uint32_t time_ms)
{
#define THREAD_STACK_SIZE   512
#define THREAD_PRIORITY     5
#define THREAD_TIMESLICE    10

    rt_thread_t tid = RT_NULL;
    /* 创建线程 1 */
    tid = rt_thread_create("delay_reboot",
                            thread_entry, (void*)time_ms,
                            THREAD_STACK_SIZE,
                            THREAD_PRIORITY, THREAD_TIMESLICE);
    if (tid != RT_NULL)
        rt_thread_startup(tid);
}

